class EnemyPlane(pygame.sprite.Sprite):
    # 敌方所有子弹
    enemy_bullets = pygame.sprite.Group()

    def __init__(self, screen):
        # 这个精灵的初始化方法 必须调用
        pygame.sprite.Sprite.__init__(self)

        # 载入敌方飞机图片
        self.player = pygame.image.load("enemy0.png")

        # 根据图片imgae获取矩形对象
        self.rect = self.player.get_rect()

        x = random.randrange(1, Manager.bg_size[0], 50)
        self.rect.topleft = [x, 0]

        # 飞机速度
        self.speed = 2

        # 记录当前的窗口对象
        self.screen = screen

        # 装子弹的列表
        self.bullets = pygame.sprite.Group()

        # 敌机移动方向
        self.direction = "right"

    def display(self):
        # 将玩家飞机显示到窗口中
        self.screen.blit(self.player, self.rect)

        # 更新子弹坐标
        self.bullets.update()

        # 把所有子弹全部添加到屏幕
        self.bullets.draw(self.screen)

    def auto_move(self):
        if self.direction == "right":
            self.rect.right += self.speed
        elif self.direction == "left":
            self.rect.right -= self.speed

        if self.rect.right >= Manager.bg_size[0]:
            self.direction = "left"
        elif self.rect.left < 0:
            self.direction = "right"

        self.rect.bottom += self.speed

    def auto_fire(self):
        """自动开火 创建子弹对象 添加进列表"""
        random_num = random.randint(1, 10)
        if random_num == 3:
            bullet = EnemyBullet(self.screen, self.rect.left, self.rect.top)
            self.bullets.add(bullet)
            # 把子弹添加到类属性的子弹组里
            EnemyPlane.enemy_bullets.add(bullet)

    def update(self):
        self.auto_move()
        self.auto_fire()
        self.display()

    @classmethod
    def clear_bullets(cls):
        # 清空子弹
        cls.enemy_bullets.empty()